<template>
    <div class="formContainer">
        <el-form :model="conditionForm" :rules="rules" ref="formRef" label-width="auto" class="conditionForm demoBlock">
            <el-form-item label="组织代码" prop="organizationCode">
                <el-input v-model="conditionForm.organizationCode"></el-input>
            </el-form-item>
            <el-form-item label="成本类型" prop="costTypeId">
                <el-select v-model="conditionForm.costTypeId" placeholder="请选择活动区域">
                    <el-option v-for="(item) in costType" :label="item.costType" :key="item.businessId"
                        :value="`${item.businessId}`"></el-option>
                </el-select>
            </el-form-item>
            <slot name="addtionItem"></slot>
            <el-form-item>
                <el-row style="float:right">
                    <el-button @click="resetForm">重置</el-button>
                    <el-button type="primary" @click="submitForm">提交</el-button>
                </el-row>

            </el-form-item>
        </el-form>
    </div>
</template>

<script lang="ts" setup>
import { accumulateBOM, getCostType, updateCost } from '@/requests/api/material';
import { Message } from 'element-ui';
import { Ref, onMounted, ref } from 'vue';
import { useCostType } from '@/hooks/useCostType';

const props = defineProps({
    conditionForm: {
        type: Object, default: {
            organizationCode: "",
            costTypeId: ""
        }
    },
    additionRules: Object,
    formName: String,
})
console.log('props', props)
const rules = ref(Object.assign({
    organizationCode: [
        { required: true, message: '请输入组织代码', trigger: 'blur' },
    ],
    costTypeId: [
        { required: true, message: '请选择成本类型', trigger: 'blur' },
    ]
}, props.additionRules))


const { costType, getCostT } = useCostType(getCostType)
onMounted(async () => {
    getCostT()
})
const emit = defineEmits(['changeProps'])
const changeProps = () => {
    emit('changeProps', props.conditionForm)
}
const formRef: Ref<any> = ref(null)
const submitForm = async () => {
    formRef.value.validate(async (valid: any) => {
        if (valid) {
            let res;
            console.log('条件值', props.conditionForm)
            changeProps()
            switch (props.formName) {
                case 'update':
                    res = await updateCost(props.conditionForm)
                    break;
                case 'add':
                    res = await accumulateBOM(props.conditionForm)
                    break;
                default:
                    break;
            }
            if (res && res.data) {
                Message.success('操作成功');
                resetForm();
            } else {
                Message.error('操作失败');
            }
        } else {
            Message.error('请将表单填写完整');
            return false;
        }
    });
}
const resetForm = () => {
    formRef.value.resetFields();
}
</script>
<style lang="scss" scoped>
.formContainer {
    .conditionForm {
        width: 500px;
        padding: 10px;

        .el-form-item__label {}

        .el-select {
            width: 100%;
        }
    }
}
</style>
